﻿using System.Collections;
using System.Collections.Generic;
using System;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(EffectControl))]
    public sealed class EffectControlEditor : Editor
    {
        private static HashSet<EffectControl> effCtlList = new HashSet<EffectControl>();
        private SerializedProperty looping;
        private SerializedProperty delay;
        private SerializedProperty duration;
        private SerializedProperty fadeout;
        private static bool IsPlay = false;
        private static bool IsLooping = true;

        private float curPlayTime;
        private double timeSinceStartup = -1.0f;
        private void OnEnable()
        {
            SerializedObject serializedObject = base.serializedObject;
            this.looping = serializedObject.FindProperty("looping");
            this.delay = serializedObject.FindProperty("delay");
            this.duration = serializedObject.FindProperty("duration");
            this.fadeout = serializedObject.FindProperty("fadeout");
            if (PrefabUtility.GetPrefabType(this.target) == PrefabType.Prefab)
            {
                return;
            }
            EffectControl effectControl = this.target as EffectControl;
            if (effectControl == null)
            {
                return;
            }
            if (!effCtlList.Contains(effectControl))
            {
                effCtlList.Add(effectControl);
                EditorApplication.update += OnUpdate;
                this.timeSinceStartup = EditorApplication.timeSinceStartup;
                effectControl.SimulateInit();
            }
        }

        public override void OnInspectorGUI()
        {
            EffectControl effectControl = (EffectControl)this.target;
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.looping, new GUIContent("Looping"), new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.delay, new GUIContent("Delay Time"), new GUILayoutOption[0]);
            if (!this.looping.boolValue)
            {
                EditorGUILayout.PropertyField(this.duration, new GUIContent("Duration Time"), new GUILayoutOption[0]);
            }
            EditorGUILayout.PropertyField(this.fadeout, new GUIContent("Fade Out Time"), new GUILayoutOption[0]);
            if (GUILayout.Button("Estimate", new GUILayoutOption[0]))
            {
                effectControl.EstimateDuration();
            }
            base.serializedObject.ApplyModifiedProperties();
            if (PrefabUtility.GetPrefabType(this.target) == PrefabType.Prefab)
            {
                return;
            }
            EditorGUILayout.Space();
            if (GUILayout.Button("Refresh", new GUILayoutOption[0]))
            {
                effectControl.Clear();
            }
            if (Application.isPlaying)
            {
                this.DrawPreviewUI(effectControl);
            }
            else
            {
                this.u1680(effectControl);
            }
        }

        private void DrawPreviewUI(EffectControl effctl)
        {
            EditorGUILayout.LabelField("Preview:", new GUILayoutOption[0]);
            GUILayoutEx.BeginContents();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            if (effctl.IsStopped)
            {
                if (GUILayout.Button("Play", new GUILayoutOption[0]))
                {
                    effctl.FinishEvent += () => { this.Repaint(); };
                    effctl.Reset();
                    effctl.Play();
                }
            }
            else if (effctl.IsPaused)
            {
                if (GUILayout.Button("Resume", new GUILayoutOption[0]))
                {
                    effctl.Resume();
                }
            }
            else if (GUILayout.Button("Pause", new GUILayoutOption[0]))
            {
                effctl.Pause();
            }
            GUI.enabled = !effctl.IsStopped;
            if (GUILayout.Button("Stop", new GUILayoutOption[0]))
            {
                effctl.Stop();
            }
            GUI.enabled = true;
            GUILayout.EndHorizontal();
            effctl.PlaybackSpeed = EditorGUILayout.Slider("Playback Speed:", effctl.PlaybackSpeed, 0f, 5f, new GUILayoutOption[0]);
            GUILayoutEx.EndContents();
        }

        private void u1680(EffectControl effctl)
        {
            GUILayoutEx.BeginContents();
            EditorGUI.BeginChangeCheck();
            this.curPlayTime = EditorGUILayout.Slider("Playing Time:", this.curPlayTime, 0f, effctl.Duration + effctl.Fadeout, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck())
            {
                if (IsPlay)
                {
                    effctl.SimulateStart();
                    effctl.SimulateDelta(this.curPlayTime, this.curPlayTime);
                    this.timeSinceStartup = EditorApplication.timeSinceStartup;
                }
                else
                {
                    effctl.Simulate(this.curPlayTime);
                }
                SceneView.RepaintAll();
                this.Repaint();
            }
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            EditorGUI.BeginChangeCheck();
            IsPlay = EditorGUILayout.ToggleLeft("Playing", IsPlay, new GUILayoutOption[0]);
            if (EditorGUI.EndChangeCheck() && IsPlay)
            {
                effctl.SimulateStart();
            }
            IsLooping = EditorGUILayout.ToggleLeft("Looping", IsLooping, new GUILayoutOption[0]);
            GUILayout.EndHorizontal();
            GUILayoutEx.EndContents();
        }

        private void OnDestroy()
        {
            EffectControl effectControl = this.target as EffectControl;
            if (effectControl != null && effCtlList.Contains(effectControl))
            {
                effCtlList.Remove(effectControl);
            }
            EditorApplication.update -= OnUpdate;
        }



        private void OnUpdate()
        {
            EffectControl effectControl = (EffectControl)this.target;
            if (effectControl == null)
            {
                return;
            }
            if (PrefabUtility.GetPrefabType(this.target) == PrefabType.Prefab)
            {
                return;
            }
            float num = (float)(EditorApplication.timeSinceStartup - this.timeSinceStartup);
            this.timeSinceStartup = EditorApplication.timeSinceStartup;
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }
            if (!EffectControlEditor.IsPlay)
            {
                return;
            }
            this.curPlayTime += num;
            if (EffectControlEditor.IsLooping && this.curPlayTime > effectControl.Duration + effectControl.Fadeout)
            {
                this.curPlayTime = 0f;
                effectControl.SimulateStart();
            }
            effectControl.SimulateDelta(this.curPlayTime, num);
            SceneView.RepaintAll();
            this.Repaint();
        }



    }
}

